package com.mervin.helper;

import com.mervin.annotation.LineParseAnnotation;
import com.mervin.constant.Constants;
import com.mervin.enums.LineParseTypeEnum;

import java.beans.IntrospectionException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;

/***************************************
 * @author: mervin
 * @Date: 2020-05-04 12:51
 * @Description:
 ***************************************/
public class ParseHandler {

    public static Object handler(String line, Class<?> clazz){

        if(!checkLine(line, clazz)){
            System.out.println("clazz " + clazz.toString() + "不符合要求");
            return null;
        }

        ParseProcess parseProcess = ProcessFactory.getProcess(clazz);
        if(parseProcess == null){
            System.out.println("找不到对应的处理类...");
            return null;
        }

        Object obj = null;
        try {
            obj = parseProcess.process(line, clazz);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return obj;
    }

    /**
     * 校验行长度
     * @param line
     * @param clazz
     * @return
     */
    public static boolean checkLine(String line, Class<?> clazz){

        if(!clazz.isAnnotationPresent(LineParseAnnotation.class)){
            return false;
        }
        LineParseAnnotation lineParseAnnotation = clazz.getDeclaredAnnotation(LineParseAnnotation.class);
        LineParseTypeEnum lineParseTypeEnum = lineParseAnnotation.parseType();

        if(lineParseTypeEnum == null){
            return false;
        }

        switch (lineParseTypeEnum){
            case SEPARATE:
                return checkSeperateLine(lineParseAnnotation, line);
            case FIXED:
                return checkFixedLenLine(lineParseAnnotation, line);
            default:
                System.out.println("===");
        }
        return false;
    }

    /**
     * 校验固定长度行长度
     * @param lineParseAnnotation
     * @param line
     * @return
     */
    private static boolean checkFixedLenLine(LineParseAnnotation lineParseAnnotation, String line) {
        int length = lineParseAnnotation.length();
        byte[] byteArray = new byte[0];
        try {
            byteArray = line.getBytes(Constants.FILE_ENCODING_UTF8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return length == byteArray.length;
    }

    /**
     * 校验拆分行长度
     * @param lineParseAnnotation
     * @param line
     * @return
     */
    private static boolean checkSeperateLine(LineParseAnnotation lineParseAnnotation, String line) {
        int length = lineParseAnnotation.length();
        String splitChar = lineParseAnnotation.splitChar();
        String[] lineArray = line.split(splitChar);
        return length == lineArray.length;
    }
}
